package com.alibaba.serverless.common;

import com.alibaba.fastjson.JSON;
import org.apache.commons.beanutils.BeanUtils;
import java.util.Map;

/**
 * 请谨慎修改此处代码
 * 请求内容，参考：https://help.aliyun.com/document_detail/54788.html
 * @author ningzhong.wyl
 *
 */
public class ApiGwRequest {
    private String path;
    private String httpMethod;
    private Map<String,Object> headers;
    private Map<String,Object> queryParameters;
    private Map<String,Object> pathParameters;
    private String body;
    private boolean isBase64Encoded;

    public Object getBodyJsonToClass(){
        return JSON.parseObject(body);
    }
    /**
     * 封装传参到对象
     * @param beanClass
     * @return 请求对象
     * @throws Exception
     */
    public Object initQueryToClass(Class<?> beanClass)  {
        if (queryParameters == null) {
            return null;
        }
        try {
            Object obj= beanClass.newInstance();
            BeanUtils.populate(obj, queryParameters);
            return obj;
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return null;
    }

    public Object getParam(String paramsName){
        if (null!=queryParameters){
            return queryParameters.get(paramsName);
        }
        return null;
    }
    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getHttpMethod() {
        return httpMethod;
    }

    public void setHttpMethod(String httpMethod) {
        this.httpMethod = httpMethod;
    }

    public Map<String, Object> getHeaders() {
        return headers;
    }

    public void setHeaders(Map<String, Object> headers) {
        this.headers = headers;
    }

    public Map<String, Object> getQueryParameters() {
        return queryParameters;
    }

    public void setQueryParameters(Map<String, Object> queryParameters) {
        this.queryParameters = queryParameters;
    }

    public Map<String, Object> getPathParameters() {
        return pathParameters;
    }

    public void setPathParameters(Map<String, Object> pathParameters) {
        this.pathParameters = pathParameters;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public boolean getIsBase64Encoded() {
        return isBase64Encoded;
    }

    public void setIsBase64Encoded(boolean isBase64Encoded) {
        this.isBase64Encoded = isBase64Encoded;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("ApiGwRequest{");
        sb.append("path='").append(path).append('\'');
        sb.append(", httpMethod='").append(httpMethod).append('\'');
        sb.append(", headers=").append(headers);
        sb.append(", queryParameters=").append(queryParameters);
        sb.append(", pathParameters=").append(pathParameters);
        sb.append(", body='").append(body).append('\'');
        sb.append(", isBase64Encoded=").append(isBase64Encoded);
        sb.append('}');
        return sb.toString();
    }
}
